跳到主要内容

Node.js 批量下载文件

1 分钟阅读

概述

在平时如果遇到需要批量下载连续的文件,我们可以写一段脚本,让其自动下载。

开始

const fs = require("fs");
const request = require("request");

const syncDownload = (url, path) => {
return new Promise((resolve) => {
let writeStream = fs.createWriteStream(path);
let readStream = request(url);
readStream.pipe(writeStream);
readStream.on("end", function (err) {
if (err) {
console.log(err);
resolve("err");
return;
}
resolve("ok");
});
});
};
const errs = [];
//需要爬取的数据
const data = [
{
url: "",
filename: "",
},
];
async function downloadFile() {
for (let i = 0, length = data.length; i < length; i++) {
let result = await syncDownload(data[i].url, `./save/${data[i].filename}`);
if (result === "ok") {
console.log(`${i + 1} 个文件下载成功`);
} else {
console.log(`${i + 1} 个文件下载失败!!!`);
errs.push(data[i]);
}
}
console.log(errs);
console.log(`共有 ${errs.length} 个文件下载失败`);
}
downloadFile();
评论
0条评论

添加新评论

昵称
邮箱
网址